//
//  MainController.swift
//  chaorenWeibo
//
//  Created by tanchao on 15/7/28.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit

class MainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewController()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setUpComposeButton()
        
    }
//   MARK:- 监听发微博按钮
//    运行循环监听事件后，向VC发送消息，动态执行方法，因此不能设置为 private
    func composeButtonClick(){
        print(__FUNCTION__)
    }
    private func setUpComposeButton(){
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)

    }
//    MARK:- 设置子控制器信息
    private func addChildViewController() {
        addChildViewController(HomeController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageController(), title: "消息",imageName:"tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverController(), title: "发现",imageName:"tabbar_discover")
        addChildViewController(ProfileController(), title: "我",imageName:"tabbar_profile")
    }
    private func addChildViewController(viewController : UIViewController,title : String,imageName : String) {
        tabBar.tintColor = UIColor.orangeColor()
        viewController.title = title
        viewController.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: viewController)
        addChildViewController(nav)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//    MARK:- 懒加载发微博按钮
    lazy private  var composeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
           self.tabBar.addSubview(btn)
        btn.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()

}
